{% from "dockers/dockerfile-macros.j2" import install_debian_packages, install_python_wheels, copy_files %}
ARG BASE=docker-syncd-mrvl-teralynx-{{DOCKER_USERNAME}}:{{DOCKER_USERTAG}}

FROM $BASE AS base

## Make apt-get non-interactive
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get purge -y syncd

## Pre-install the fundamental packages
RUN apt-get update \
 && apt-get -y install  \
    net-tools           \
    python3-pip          \
    python3-setuptools   \
    build-essential     \
    libssl-dev          \
    libffi-dev          \
    python3-dev          \
    libjansson4         \
    libyaml-dev         \
    wget                \
    cmake               \
    libthrift-0.17.0      \
    libnanomsg-dev

RUN pip3 install cffi \
 && pip3 install nnpy   \
 && mkdir -p /opt       \
 && cd /opt             \
 && wget https://raw.githubusercontent.com/p4lang/ptf/master/ptf_nn/ptf_nn_agent.py

COPY ["ptf_nn_agent.conf", "/etc/supervisor/conf.d/"]

{% if docker_syncd_mrvl_teralynx_rpc_whls.strip() -%}
# Copy locally-built Python wheel dependencies
{{ copy_files("python-wheels/", docker_syncd_mrvl_teralynx_rpc_whls.split(' '), "/python-wheels/") }}

# Install locally-built Python wheel dependencies
{{ install_python_wheels(docker_syncd_mrvl_teralynx_rpc_whls.split(' ')) }}
{% endif %}

{% if docker_syncd_mrvl_teralynx_rpc_debs.strip() -%}
# Copy built Debian packages
{{ copy_files("debs/", docker_syncd_mrvl_teralynx_rpc_debs.split(' '), "/debs/") }}
# Install built Debian packages and implicitly install their dependencies
{{ install_debian_packages(docker_syncd_mrvl_teralynx_rpc_debs.split(' ')) }}
{%- endif %}

## Clean up
RUN apt-get purge -y libyaml-dev python3-dev libffi-dev libssl-dev wget cmake \
    build-essential
RUN rm -rf /root/deps

FROM $BASE

RUN --mount=type=bind,from=base,target=/changes-to-image rsync -axAX --no-D --exclude=/sys --exclude=/proc --exclude=/dev --exclude=resolv.conf /changes-to-image/ /

ENV DEBIAN_FRONTEND=noninteractive
ENTRYPOINT ["/usr/local/bin/supervisord"]
